import {
  Controller,
  Post,
  UseGuards,
  UploadedFile,
  UseInterceptors,
  HttpCode,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { ApiTags, ApiHeader } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { UploadService } from './upload.service';

@ApiTags('文件上传模块')
@Controller('upload')
export class UploadController {
  constructor(private readonly uploadService: UploadService) {}

  @ApiHeader({
    name: 'access-token',
    required: true,
    description: 'token',
  })
  @HttpCode(200)
  @Post('/uploadArticleCover')
  @UseGuards(AuthGuard('jwt'))
  @UseInterceptors(FileInterceptor('file'))
  // 图片上传
  uploadArticleCover(@UploadedFile() file) {
    if (
      !['image/jpg', 'image/jpeg', 'image/png', 'image/bmp'].includes(
        file.mimetype,
      )
    ) {
      return { code: 1, msg: '文件类型错误' };
    }
    return this.uploadService.uploadImgToBucket(file);
  }

  @ApiHeader({
    name: 'access-token',
    required: true,
    description: 'token',
  })
  @HttpCode(200)
  @Post('/uploadVideo')
  @UseGuards(AuthGuard('jwt'))
  @UseInterceptors(FileInterceptor('file'))
  // 视频上传
  uploadVideo(@UploadedFile() file) {
    if (!['video/mp4', 'video/3gp'].includes(file.mimetype)) {
      return { code: 1, msg: '文件类型错误' };
    }
    console.log('视频上传断点');
    return this.uploadService.uploadVideoToBucket(file);
  }
}
